package com.sqx.modules.goods.controller.app;

import com.sqx.common.annotation.SysLog;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goods.service.GoodsService;
import com.sqx.modules.goods.vo.GoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品-用户端
 *
 * @author wuChang
 * @since 2024-05-30
 */
@RestController
@Api(value = "商品-用户端", tags = {"商品-用户端"})
@RequestMapping("/app/goods/")
public class AppGoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 发布或更新商品
     *
     * @param goods 商品
     */
    @SysLog(value = "发布或更新商品", type = "2")
    @Login
    @ApiOperation("发布或更新商品")
    @PostMapping(value = "saveGoods")
    public Result saveGoods(@ApiParam("用户id") @RequestAttribute Long userId, @ApiParam("商品实体") @RequestBody Goods goods) {
        if (goods.getGoodId() != null) {
            Goods oldGoods = goodsService.getById(goods.getGoodId());
            if (oldGoods == null) {
                return Result.error("商品不存在");
            }
            if (userId != null) {
                if (!oldGoods.getUserId().equals(userId)) {
                    return Result.error("您无权操作此商品");
                }
            }
        }
        goods.setStatus(0);
        goods.setIsEnable(1);
        goods.setUserId(userId);
        return goodsService.saveGoods(goods);
    }

    /**
     * 获取商品列表
     *
     * @param goods 商品
     */
    @Login
    @ApiOperation("获取我的商品列表")
    @GetMapping(value = "getMyGoodsList")
    public Result getMyGoodsList(@ApiParam("用户id") @RequestAttribute Long userId,
                                 @ApiParam("页码") Integer page,
                                 @ApiParam("条数") Integer limit,
                                 @ApiParam("商品实体") Goods goods) {
        goods.setUserId(userId);
        return Result.success().put("data", goodsService.getMyGoodsList(page, limit, goods));
    }

    /**
     * @param page  第几页
     * @param limit 多少条
     * @return
     */
    @ApiOperation("获取商品列表")
    @GetMapping(value = "getGoodsList")
    public Result getGoodsList(@ApiParam("页码") Integer page,
                               @ApiParam("条数") Integer limit,
                               @ApiParam("商品的实体") GoodsVo goodsVo) {
        return Result.success().put("data", goodsService.getGoodsList(page, limit, goodsVo));
    }

    /**
     * searchType 1.综合 2.时间 3.价格 4.区域
     *
     * @param page  第几页
     * @param limit 多少条
     * @return
     */
//    @Login
    @ApiOperation("搜索查询商品列表")
    @GetMapping(value = "getSearchGoodsList")
    public Result getSearchGoodsList(
//            @ApiParam("用户id") @RequestAttribute Long userId,
            @ApiParam("页码") Integer page,
            @ApiParam("条数") Integer limit,
            @ApiParam("商品的实体") GoodsVo goodsVo) {
//        goodsVo.setUserId(userId);
        return Result.success().put("data", goodsService.getGoodsBySearchList(page, limit, goodsVo));
    }

    /**
     * @param page  第几页
     * @param limit 多少条
     * @return
     */
    @ApiOperation("获取标签分类下的商品列表")
    @GetMapping(value = "getGoodsListByLabel")
    public Result getGoodsListByLabel(@ApiParam("页码") Integer page,
                                      @ApiParam("条数") Integer limit,
                                      @ApiParam("商品的实体") GoodsVo goodsVo) {
        return Result.success().put("data", goodsService.getGoodsListByLabel(page, limit, goodsVo));
    }


    /**
     * 获取附近捡漏的商品列表(价格由低到高)
     *
     * @param page  第几页
     * @param limit 多少条
     * @return
     */
    @ApiOperation("获取附近捡漏的商品列表(价格由低到高)")
    @GetMapping(value = "getGoodsListByPrice")
    public Result getGoodsListByPrice(@ApiParam("页码") Integer page,
                                      @ApiParam("条数") Integer limit,
                                      @ApiParam("商品的实体") GoodsVo goodsVo) {
        return Result.success().put("data", goodsService.getGoodsListByPrice(page, limit, goodsVo));
    }


    /**
     * 商品上下架
     *
     * @param goodId 商品ID
     * @return
     */
    @Login
    @ApiOperation("商品上下架")
    @SysLog(value = "商品上下架", type = "2")
    @GetMapping(value = "updateStatus")
    public Result updateStatus(@ApiParam("用户id") @RequestAttribute Long userId,
                               @ApiParam("商品id") Long goodId) {
        return goodsService.updateStatus(userId, goodId);
    }

    /**
     * 删除商品
     *
     * @param goodId 商品id
     */
    @ApiOperation("删除商品")
    @SysLog(value = "删除商品", type = "2")
    @PostMapping(value = "deleteGoodsById")
    public Result deleteGoodsById(@ApiParam("商品id") Long goodId) {

        return goodsService.removeById(goodId) ? Result.success() : Result.error();
    }


    /**
     * 获取商品详情
     *
     * @param goodId 商品id
     */
    @ApiOperation("获取商品详情")
    @GetMapping(value = "getGoodsById")
    public Result getGoodsById(@ApiParam("用户id") Long userId,
                               @ApiParam("商品id") Long goodId,
                               @ApiParam("用户自己的id") Long loginUserId) {
        return Result.success().put("data", goodsService.getAppGoodsById(userId, goodId, loginUserId));
    }


    @ApiOperation("根据经纬度获取当前经纬度下的商品列表")
    @GetMapping(value = "getGoodsByCoordinates")
    public Result getGoodsByCoordinates(@ApiParam("商品类型id") Long typeId,
                                        @ApiParam("页码") Integer pageNum,
                                        @ApiParam("条数") Integer pageSize,
                                        @ApiParam("商品的实体") GoodsVo goodsVo) {
        return goodsService.getGoodsByCoordinates(typeId, pageNum, pageSize, goodsVo);
    }

    @ApiOperation("根据城市名称搜索商品列表")
    @GetMapping(value = "getGoodsListByCity")
    public Result getGoodsListByCity(@ApiParam("页码") Integer page,
                                     @ApiParam("条数") Integer limit,
                                     @ApiParam("商品的实体") GoodsVo goodsVo) {
        return Result.success().put("data", goodsService.getGoodsListByCity(page, limit, goodsVo));
    }

    @ApiOperation("根据名称查一级分类")
    @GetMapping(value = "selectTypeByName")
    public Result selectTypeByName(@ApiParam("分类名称") String typeName) {
        return goodsService.selectTypeByName(typeName);
    }

    @ApiOperation("根据一级分类查二级分类")
    @GetMapping(value = "selectByType")
    public Result selectByType(@ApiParam("分类id") Long typeId) {
        return goodsService.selectByType(typeId);
    }

    @ApiOperation("根据二级分类查品牌")
    @GetMapping(value = "selectBrandByTypeId")
    public Result selectBrandByTypeId(@ApiParam("商品类型id") Long typeId,
                                      @ApiParam("品牌名称") String brandName) {
        return goodsService.selectBrandByTypeId(typeId, brandName);
    }

    @ApiOperation("查询该商品的新旧程度")
    @GetMapping(value = "selectNewOld")
    public Result selectNewOld() {
        return goodsService.selectNewOld();
    }

    @Login
    @ApiOperation("选择是否置顶商品")
    @PostMapping(value = "isTopGoods")
    public Result isTopGoods(@ApiParam("用户id") @RequestAttribute Long userId,
                             @ApiParam("商品id") Long goodId) {
        return goodsService.isTopGoods(userId, goodId);
    }

    @ApiOperation("根据商品id查询商品")
    @GetMapping("selectByGoodId")
    public Result selectByGoodId(@ApiParam("商品id") Long goodId) {
        return goodsService.selectByGoodId(goodId);
    }

    /**
     * 修改库存
     *
     * @param goods 商品的实体
     * @return
     */
    @ApiOperation("修改库存")
    @PostMapping(value = "updateByStock")
    public Result updateByStock(@ApiParam("商品的实体") Goods goods) {
        return goodsService.updateByStock(goods);
    }

    /**
     * 查询所有一级分类
     *
     * @return
     */
    @ApiOperation("查询所有一级分类")
    @GetMapping(value = "getGoodsByTypes")
    public Result getGoodsByTypes(Integer page, Integer limit, String typeName) {
        return goodsService.getGoodsByTypes(page, limit, typeName);
    }

}